/*
 * Pereferences.java
 *
 * Created on __DATE__, __TIME__
 */

package edu.washburn.cm333.project.gui;

import java.awt.Cursor;
import java.awt.Dialog;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.swing.JOptionPane;

import edu.washburn.cm333.project.entities.Lease;
import edu.washburn.cm333.project.entities.PropertyManager;

/**
 *
 * @author  __USER__
 */
public class Preferences extends javax.swing.JFrame {

	/** Creates new form Pereferences */
	public Preferences() {
		initComponents();
	}

	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jButton1 = new javax.swing.JButton();
		jButton2 = new javax.swing.JButton();
		jButton3 = new javax.swing.JButton();
		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		jTextField1 = new javax.swing.JTextField();
		jTextField2 = new javax.swing.JTextField();
		jTextField3 = new javax.swing.JTextField();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		jButton1.setText("Save");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		jButton2.setText("Create Tables");
		jButton2.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton2ActionPerformed(evt);
			}
		});

		jButton3.setText("Exit");
		jButton3.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton3ActionPerformed(evt);
			}
		});

		jLabel1.setText("URL");

		jLabel2.setText("User Name");

		jLabel3.setText("Password");

		jTextField1.setText("jdbc:mysql://localhost:3306/test");
		jTextField1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jTextField1ActionPerformed(evt);
			}
		});

		jTextField2.setText("root");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(28, 28, 28)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.TRAILING,
												false)
												.addGroup(
														javax.swing.GroupLayout.Alignment.LEADING,
														layout.createSequentialGroup()
																.addComponent(
																		jLabel3)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(
																		jTextField3))
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		jLabel2)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(
																		jTextField2,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		196,
																		javax.swing.GroupLayout.PREFERRED_SIZE)))
								.addGap(15, 15, 15))
				.addGroup(
						layout.createSequentialGroup().addContainerGap()
								.addComponent(jButton1).addGap(18, 18, 18)
								.addComponent(jButton2).addGap(18, 18, 18)
								.addComponent(jButton3))
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addContainerGap(68, Short.MAX_VALUE)
								.addComponent(jLabel1)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jTextField1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										196,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(20, 20, 20)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(jLabel1)
												.addComponent(
														jTextField1,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel2)
												.addComponent(
														jTextField2,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel3)
												.addComponent(
														jTextField3,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jButton1)
												.addComponent(jButton2)
												.addComponent(jButton3))
								.addContainerGap(
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
		Preferences.this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
		Map<String, String> oldMap = PropertyManager.readProperties();
		Map<String, String> newMap = PropertyManager.readProperties();
		newMap.put("eclipselink.ddl-generation", "create-tables");
		newMap.put("eclipselink.ddl-generation.output-mode", "database");
		PropertyManager.setProperties(newMap);
		EntityManager em = PropertyManager.getEntityManager();
		if(em.isOpen()){
			em.close();
			em = PropertyManager.getEntityManager();
		}
		try{
		em.createQuery("select t from Lease t",	Lease.class).getResultList();
		}catch(Exception e){
			e.printStackTrace();
			//TODO: work on this here
			JOptionPane.showMessageDialog(null, "Error Creating tables.");
		}
		em.close();

		PropertyManager.setProperties(oldMap);

		Preferences.this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
	}

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
		String url = jTextField1.getText().trim();
		String user = jTextField2.getText().trim();
		String password = jTextField3.getText().trim();

		Map<String, String> addedOrOverridenProperties = new HashMap<String, String>();

		if (!url.isEmpty())
			addedOrOverridenProperties.put("javax.persistence.jdbc.url", url);
		if (!user.isEmpty())
			addedOrOverridenProperties.put("javax.persistence.jdbc.user", user);
		if (!password.isEmpty())
			addedOrOverridenProperties.put("javax.persistence.jdbc.password",
					password);
		PropertyManager.setProperties(addedOrOverridenProperties);

		EntityManager em = PropertyManager.getEntityManager();
		if(em.isOpen())
			em.close();
	}

	private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
		setVisible(false);
	}

	private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new Preferences().setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton jButton1;
	private javax.swing.JButton jButton2;
	private javax.swing.JButton jButton3;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JTextField jTextField1;
	private javax.swing.JTextField jTextField2;
	private javax.swing.JTextField jTextField3;
	// End of variables declaration//GEN-END:variables

}